﻿using Max.PH5_2010A.Shopping.Domain;
using Max.PH5_2010A.Shopping.DTO;
using Max.PH5_2010A.Shopping.IRespository;
using Max.PH5_2010A.Shopping.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2010A.Shopping.Service
{
    public class SystemCityService : BaseService<SystemCityModel>, ISystemCityService
    {
        ISystemCityRespository _respository;
        public SystemCityService(ISystemCityRespository respository, IUnitOfWork unitOfWork) : base(respository, unitOfWork)
        {
            _respository = respository;
        }


        public List<SystemCityRecursionDto> GetRecursionCity()
        {
            var data = _respository.Select();
            var list = this.GetRecursionCity(data, 0);
            return list;
        }

        /// <summary>
        /// 获取城市列表的递归数据
        /// </summary>
        /// <param name="id"></param>
        private List<SystemCityRecursionDto> GetRecursionCity(IQueryable<SystemCityModel> data, int id)
        {
            var list = data.Where(x => x.ParentId == id);
            List<SystemCityRecursionDto> result = new();
            list.ToList().ForEach(x =>
            {
               var tem =   GetRecursionCity(data,x.Id);

                result.Add(new SystemCityRecursionDto
                {
                    Value = x.Id.ToString(),
                    Label = x.Name,
                    Children = tem == null || tem.Count() <= 0 ? null : tem

                });
            });

            return result;
        }
    }
}
